//
//  YBProjectDetailViewController.m
//  ZhongWangYuanApp
//
//  Created by apple on 18/6/3.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YBProjectDetailViewController.h"
#import "YBLearnerViewController.h"
#import "YBClassDetailViewController.h"
#import "YBClassProgressViewController.h"
#import "YBExpandViewController.h"
#import "YBObjectIntroductionModel.h"
#import "LPZWYChooseClassClassModel.h"

@interface YBProjectDetailViewController ()
/**    */
@property (strong, nonatomic) GTPageController  *gtPageVc;

@property (nonatomic , strong) UIImageView * headImageView;

@property (nonatomic , strong) YBObjectIntroductionModel * introductionModel;
@end

@implementation YBProjectDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = self.theModel.name;
    [self initDataSource];
    
}

-(void)dealloc
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"phonerichmediaurl"];
}




-(void)initDataSource{
    [YJProgressHUD showMessage:@"加载中..." duration:10.f view:self.view];
    [YBRequestManager request_objectDetailWithIdString:self.theModel.ID successBlock:^(id object) {
        if ([object[@"code"] integerValue] == 1) {
            if ([object[@"data"] isKindOfClass:[NSArray class]]) {
                self.introductionModel = [YBObjectIntroductionModel yy_modelWithJSON:object[@"data"][0]];
                NSString * urlString = @"";
                if (self.introductionModel.phonerichmediaurl.length > 0) {
                    urlString = [self.introductionModel.phonerichmediaurl stringByReplacingOccurrencesOfString:@"{{classid}}" withString:self.introductionModel.ID];
                }
                NSString * tempTitleString = self.theModel.name.length > 0 ? self.theModel.name : @"";
                [[NSUserDefaults standardUserDefaults] setObject:@{@"url" : urlString , @"title" : tempTitleString} forKey:@"phonerichmediaurl"];
                [self requestIsSignUp];
            }
        }
        else{
            [YJProgressHUD showMsgWithoutView:@"获取失败"];
        }
    } failBlock:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"获取失败"];
    }];
}


-(void)requestIsSignUp{
    [YBRequestManager request_objectIsSignInWithIdString:self.theModel.ID successBlock:^(id object) {
        if ([object[@"code"] integerValue] == 1) {
            if ([object[@"data"] isKindOfClass:[NSArray class]]) {
                NSArray * tempArray = object[@"data"];
                if (tempArray.count > 0) {
                    self.introductionModel.issignin = @"1";
                }
                [self synchronousClassData];
            }
            else{
                [YJProgressHUD showMsgWithoutView:@"获取失败"];
            }
        }
        else{
            [YJProgressHUD showMsgWithoutView:@"获取失败"];
        }
    } failBlock:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"获取失败"];
    }];
}

#pragma mark - 同步班级进度数据
-(void)synchronousClassData{
    [YBRequestManager request_synchronousClassDataWithIdString:self.theModel.ID successBlock:^(id object) {
        [YJProgressHUD hide];
        if ([object[@"code"] integerValue] == 1) {
            if ([object[@"data"][@"result"] integerValue] == 1) {
                [self layoutUI];
            }
            else{
                [YJProgressHUD showMsgWithoutView:object[@"data"][@"message"]];
            }
        }
        else{
            [YJProgressHUD showMsgWithoutView:@"同步失败"];
        }
    } failBlock:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"同步失败"];
    }];
}



#pragma mark - 界面

- (void)layoutUI{
    self.titlelabel.text = [NSString stringWithFormat:@"%@" , self.introductionModel.name];
    [self.headImageView sd_setImageWithURL:CREATEURL(self.introductionModel.image_servername) placeholderImage:PlaceholderBIGImage];
    self.gtPageVc = [self getDefaultController];
    self.gtPageVc.Notscroll = YES;
    self.gtPageVc.view.frame = CGRectMake(0, LP_NavViewHeight + ItemHeight * 465 , self.view.width, [UIScreen zj_screenHeight] - LP_NavViewHeight - ItemHeight * 465 );
    [self.view addSubview:self.gtPageVc.view];
    [self.view addSubview:self.headImageView];
    [self addChildViewController:self.gtPageVc];
    
    self.headImageView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.navigView , 0)
    .heightIs(465 * ItemHeight);
    
    self.gtPageVc.view.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.headImageView , 0)
    .bottomSpaceToView(self.view , 0);
    
    self.gtPageVc.selectIndex = 1;
    
    
}



#pragma mark - 私有方法
- (GTPageController *)getDefaultController{
    
    YBLearnerViewController *learnerVC = [YBLearnerViewController new];
    YBClassDetailViewController * detailVC = [YBClassDetailViewController new];
    YBExpandViewController *expandVC = [YBExpandViewController new];
    YBClassProgressViewController *progressVC = [YBClassProgressViewController new];
    
    
    
    NSArray *titles = @[];
    
    NSArray *viewVcs = @[];
    
    NSArray * modelArray = @[];
    
    if ([self.introductionModel.ifopenrichmedia integerValue] == 1) {
        titles = @[@"导学" , @"课程" , @"拓展" , @"进度"];
        viewVcs = @[[learnerVC class] , [detailVC class] ,[expandVC class] ,[progressVC class]];
        modelArray = @[self.introductionModel , self.introductionModel , self.introductionModel , self.introductionModel];
    }
    else{
        titles = @[@"导学" , @"课程" , @"进度"];
        viewVcs = @[[learnerVC class] , [detailVC class] ,[progressVC class]];
        modelArray = @[self.introductionModel , self.introductionModel , self.introductionModel];
    }
    
    __block NSMutableArray *titlesWidth = [NSMutableArray array];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat width = [NSString sizeOfString:obj font:16 * [UIScreen zj_textFontScale]  width:self.view.width].width + 12 * [UIScreen zj_screenWScale];
        [titlesWidth addObject:@(width * 1.6)];
    }];
    
    GTPageController *pageVC = [[GTPageController alloc] initWithViewControllerClasses:viewVcs andTheirTitles:titles];
    pageVC.pageAnimatable = YES;
    pageVC.modelArray = modelArray;
    pageVC.itemsWidths = titlesWidth;
    pageVC.postNotification = YES;
    pageVC.menuBGColor = [UIColor clearColor];
    pageVC.menuViewStyle = GTMenuViewStyleLine;
    pageVC.progressColor = [UIColor themeColor];
    pageVC.titleColorSelected = [UIColor themeColor];
    pageVC.titleColorNormal = [UIColor textGrayColor];
    pageVC.titleSizeNormal = 16 * [UIScreen zj_textFontScale];
    pageVC.titleSizeSelected = 17 * [UIScreen zj_textFontScale];
    pageVC.menuHeight = 50 * [UIScreen zj_screenHScale];
    return pageVC;
}


-(UIImageView *)headImageView
{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, LP_NavViewHeight, [UIScreen zj_screenWith], ItemHeight * 465)];
    }
    return _headImageView;
}


@end
